using System;
using System.Collections;
using System.Collections.Generic;
using RootMotion.FinalIK;
using UnityEngine;

public class MotionTestController : MonoBehaviour
{
	[Serializable]
	public class HitAnimation
	{
		public AnimationClip anim;
	}

	public List<HitAnimation> hitAnimations;

	public string status;

	[Range(0f, 1f)]
	public float handInfluence;

	public Transform handTarget;

	public Transform originalTarget;

	public Transform hand;

	public Transform handIKTarget;

	public bool aimIKOn;

	[Header("Player Setup")]
	public Transform racketCentre;

	public float transitionDuration = 0.2f;

	public int repetitions = 1;

	public float timeScale = 0.2f;

	[Range(0f, 1f)]
	public float normTime;

	private Animator cachedAnimator_;

	public AimIK aimIK;

	public FullBodyBipedIK bodyIK;

	public bool useWorldCoords;

	public bool useRotation;

	public bool local;

	public AnimationCurve localToGlobal;

	protected Quaternion startRotation;

	protected Vector3 startPosition;

	public float playDuration = 0.5f;

	private Vector3 prevPos;

	public Animator cachedAnimator
	{
		get
		{
			if (cachedAnimator_ == null)
			{
				cachedAnimator_ = GetComponent<Animator>();
			}
			return cachedAnimator_;
		}
	}

	private void Awake()
	{
		startRotation = base.transform.rotation;
		startPosition = base.transform.position;
		StartCoroutine(PlayEvery(1f));
	}

	private void OnAnimatorIK()
	{
		float @float = cachedAnimator.GetFloat("IKWeight");
		cachedAnimator.SetIKPositionWeight(AvatarIKGoal.RightHand, @float);
		cachedAnimator.SetIKRotationWeight(AvatarIKGoal.RightHand, @float);
		handIKTarget.localPosition = handTarget.localPosition;
		handIKTarget.localRotation = handTarget.parent.localRotation;
		cachedAnimator.SetIKPosition(AvatarIKGoal.RightHand, handIKTarget.position);
		cachedAnimator.SetIKRotation(AvatarIKGoal.RightHand, handIKTarget.rotation);
	}

	private void LateUpdate()
	{
		bodyIK.solver.rightHandEffector.positionWeight = cachedAnimator.GetFloat("IKWeight");
		if (useWorldCoords)
		{
			Vector3 position = hand.position;
			position += handTarget.position - originalTarget.position;
			handIKTarget.position = position;
		}
		else
		{
			Vector3 vector = handTarget.position - originalTarget.position;
			Vector3 vector2 = handTarget.parent.InverseTransformDirection(vector);
			float @float = cachedAnimator.GetFloat("IKWeight");
			bodyIK.solver.rightHandEffector.rotationWeight = ((!useRotation) ? 0f : cachedAnimator.GetFloat("IKWeight"));
			if (local)
			{
				handIKTarget.localPosition = handTarget.localPosition;
			}
			else
			{
				Vector3 direction = vector2;
				Vector3 a = hand.position + hand.TransformDirection(direction);
				handIKTarget.position = Vector3.Lerp(a, hand.position + vector, localToGlobal.Evaluate(@float));
			}
			handIKTarget.localRotation = handTarget.localRotation;
		}
		if (aimIKOn)
		{
			Vector3 position = originalTarget.position;
			aimIK.solver.transform.LookAt(originalTarget.position);
			aimIK.solver.IKPosition = handTarget.position;
		}
	}

	public void AterFullBodyIK()
	{
	}

	private void OnAnimatorMove()
	{
		Vector3 direction = base.transform.InverseTransformDirection(cachedAnimator.deltaPosition);
		base.transform.position += base.transform.TransformDirection(direction);
	}

	private IEnumerator PlayEvery(float duration)
	{
		while (true)
		{
			for (int j = 0; j < repetitions; j++)
			{
				Play("HopLeft");
				yield return new WaitForSeconds(playDuration);
			}
			for (int i = 0; i < repetitions; i++)
			{
				Play("HopRight");
				yield return new WaitForSeconds(playDuration);
			}
		}
	}

	public void Play(string name)
	{
		cachedAnimator.CrossFade(name, transitionDuration, 0, 0f);
	}

	private void Update()
	{
		Time.timeScale = timeScale;
	}
}
